<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST, ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace temmoku\lib;
use \temmoku\db;
class cloud {
	private $method='login';
	private $file=null;
	private $tpl_dir=[];
	private $tpl_path='';
	function __construct()
	{
		$this->tpl_dir=$_POST['tpl_dir'];
		$this->method=$_POST['method'];
		unset($_POST['method']);
	}
	
	public function exec(){
		$_POST['cloud']=C('cloud_code');
		$url=Upgrade_URL."cloud/".$this->method;
		$res=http_request($url, $_POST);
		if($this->method=='login' || $this->method=='install' || $this->method=='pay_rmb'){
			$json=json_decode($res,true);
			if($json['code']==0){
				if($this->method=='login'){
					$array=['cloud_code'=>$json['cloud_code']];
					createconf($array,'cloud_code');
				}elseif($this->method=='install' || $this->method=='pay_rmb' ){
					$this->download($json['file']);
					//如果是模板需要另行判断
					if($_POST['from']=='tpl' && !$path){
						$row=db::select('*')->from(jab.'cloud')->where("cloud_id= '{$_POST['aid']}' ")->row();
						if(!$row){
							//插入数据
							$array=[
								'type'=>4,
								'initial_name'=>$json['initial_name'],
								'version'=>$json['version'],
								'developer'=>$json['developer'],
								'homepage'=>$json['homepage'],
								'cloud_id'=>(int)$_POST['aid']
							];
							db::insert(jab.'cloud')->cols($array)->query();
						}
						foreach($this->tpl_dir AS $dir){
							if($dir!='mvchome'){
								$path=$this->tpl_path=APP_PATH.$dir.DS;
								$this->unzip();
								if($_POST['is_modular']){
									$tpl_row=db::select('*')->from(jab.'modular')->where("dir= '$dir' ")->row();
								}else{
									$tpl_row=db::select('*')->from(jab.'plugin')->where("dir= '$dir' ")->row();
								}
								move_tpl($path.'view/',$tpl_row['initial_name'],$dir);
							}else{
								$this->tpl_path=Temmoku;
								$this->unzip();
							}
						}
					}else{
						$this->unzip();
					}
					
					unset($json['file']);
					$res= json_encode($json);
				}
			}
		}
		return $res;
	}
	
	private function download($file){
		
		$basename=basename($file);
		$path=RUNTIME_PATH."download/".$basename;
		write_dir($path);
		$file = fopen($file, 'r');
		$fh = fopen($path, 'w');
		while (!feof($file)) {
		    $output = fread($file, 8192);
		    fwrite($fh, $output);
		}
		fclose($file);
		fclose($fh);
		$this->file=$path;
	}
	
	private function unzip() {
		$file=$this->file;
		$path='';
		if($_POST['from']=='modular'){
			$path=APP_PATH;
		}elseif($_POST['from']=='plugin'){
			$path=APP_PATH."plugin/";
		}elseif($_POST['from']=='hook'){
			$path=APP_PATH."hook/";
		}elseif($_POST['from']=='tpl'){
			$path=$this->tpl_path;
		}
	  	//先判断待解压的文件是否存在
	  	if(!is_file($file)){
	   		die("文件 $filename 不存在！");
	  	}
	  	//将文件名和路径转成windows系统默认的gb2312编码，否则将会读取不到
	  	$file = iconv("utf-8","gb2312",$file);
	  	$path = iconv("utf-8","gb2312",$path);
	  	//打开压缩包
	  	$open_zip = @zip_open($file);
	  	//遍历读取压缩包里面的一个个文件
	  	while ($_file = @zip_read($open_zip)) {
	    	//如果能打开则继续
	    	if (@zip_entry_open($open_zip,$_file)) {
	      		//获取当前项目的名称,即压缩包里面当前对应的文件名
				$zip_file=@zip_entry_name($_file);
				 if(strripos($zip_file,'public')=== 0){
					$path=Temmoku_PATH;
				}elseif(strripos($zip_file,'hook')=== 0){
					$path=APP_PATH;
				}elseif(strripos($zip_file,'vendor')=== 0 ){
					$path=Temmoku_PATH."temmoku/";
				}

	      		$file_name = $path.$zip_file;
	      		//以最后一个“/”分割,再用字符串截取出路径部分
	      		$file_path = substr($file_name,0,strrpos($file_name, "/"));
	      		//如果路径不存在，则创建一个目录，true表示可以创建多级目录
	      		if(!is_dir($file_path)){
	        		mkdir($file_path,0777,true);
	     		}
	      		//如果不是目录，则写入文件
	      		if(!is_dir($file_name)){
	        		//读取这个文件
	        		$file_size = @zip_entry_filesize($_file);
	         		$file_content = @zip_entry_read($_file,$file_size);
					file_put_contents($file_name,$file_content);
					if($_POST['from']=='hook'){
						//进行安装操作
						$this->install_hook($file_name);
					}
	      		}
	     	 	//关闭当前
	      		@zip_entry_close($_file);
	    	}
	  	}
		//关闭压缩包
		@zip_close($open_zip);
	}
	
	//安装插件
	private function install_hook($flie){
		$flie=str_ireplace(APP_PATH,'',$flie);
		$arr=explode('.',$flie,2);
		$class=str_replace('/','\\',$arr[0]);
		$obj=new $class;
		$config=$obj->config;
		
		$hook_embedded_point=explode('|',$config['hook_embedded_point']);
		//入库
		foreach($hook_embedded_point as $value){
			$array=[
				'class'=>$class,
				'hook_name'=>$value,
				'content'=>$config['content'],
				'developer'=>$config['developer'],
				'homepage'=>$config['homepage'],
				'version'=>$config['version'],
				'cloud_id'=>(int)$_POST['aid'],
			];
			db::insert(jab.'hook_list')->cols($array)->query();
		}
		if ((int)method_exists($obj, 'init')) {
			$obj->init();
		}
	}	
}
?>